<?php


namespace app\api\service\order;


use app\api\model\UserCurrencyOrder;
use app\api\service\Token;
use app\lib\exception\ParameterException;
use app\api\model\User;

class CreateUserCurrencyOrder extends Token
{

    // 充值数量
    protected $itself;

    // 赠送金额
    protected $give;

    // 订单金额
    protected $price;

    // 用户信息
    protected $user;

    /**
     * 创建甜茶币支付订单
     */
    function __construct($itself,$give,$price)
    {
        $this->itself = $itself;
        $this->give = $give;
        $this->price = $price;
    }

    /**
     * 入口方法
     */
    public function entrance()
    {
        $this->user = self::getUserInfo();
        return $this->judgeData();
    }


    /**
     * 判断信息
     */
    protected function judgeData()
    {
        if($this->price <= 0) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单价格错误'
                ]
            ]);
        }
        return $this->createData();
    }

    /**
     * 创建信息
     */
    protected function createData()
    {
        $mode = UserCurrencyOrder::create([
            'openid' => $this->user['openid'],
            'itself' => $this->itself,
            'give' => $this->give,
            'price' => $this->price,
        ]);
        $mode->out_trade_no = '2t2021'.$mode->id;
        $mode->save();
        return [
            'out_trade_no' => '2t2021'.$mode->id
        ];
    }
}